﻿using System.Globalization;
using static AnyTap.Services.Helper;
namespace AnyTap.Converters;

/// <summary>
/// 枚举与单选 双向绑定 转换器
/// </summary>
public class ChangeTypeConverter : IValueConverter
{
	/// <inheritdoc/>
	public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
	{
		if(value is Enum e && targetType == typeof(string))
			return GetEnumRemark<LabelRemarkAttribute>(e);

		return System.Convert.ChangeType(value, targetType);
	}

	/// <inheritdoc/>
	public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
	{
		if(targetType.IsEnum)
			return Enum.ToObject(targetType, value);
		return System.Convert.ChangeType(value, targetType);
	}
}
